package wnhz.com.business.freeshop.activity.fourthfree;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import wnhz.com.business.R;
import wnhz.com.business.base.BaseActivity;
import wnhz.com.business.base.BaseRVAdapter;
import wnhz.com.business.base.BaseRecyclerAdapter;
import wnhz.com.business.base.BaseViewHolder;
import wnhz.com.business.groupshop.adapter.LogisticsAdapter;
import wnhz.com.business.groupshop.bean.LogisticsBean;
import wnhz.com.business.impl.ActionBarClickListener;
import wnhz.com.business.uitls.LogUtils;
import wnhz.com.business.uitls.MyCallBack;
import wnhz.com.business.uitls.Prefer;
import wnhz.com.business.uitls.Url;
import wnhz.com.business.uitls.XUtil;
import wnhz.com.business.view.TranslucentActionBarW;

public class LogisticsActivity extends BaseActivity implements ActionBarClickListener, BaseRecyclerAdapter.OnDataLoadFinish {

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.actionbar)
    TranslucentActionBarW actionbar;
    @BindView(R.id.empty_layout)
    LinearLayout layEmtpy;
    @BindView(R.id.tv_msg)
    TextView tv_msg;
    @BindView(R.id.ll_logistics)
    LinearLayout ll_logistics;
    @BindView(R.id.img)
    ImageView img;
    @BindView(R.id.tv_status)
    TextView tv_status;
    @BindView(R.id.tv_express_no)
    TextView tv_express_no;
    @BindView(R.id.tv_express_name)
    TextView tv_express_name;

    private List<LogisticsBean.ExpressBean> expressBeanList = new ArrayList<>();
    private String OrderId;
    private String jumpid;
    private LogisticsAdapter logisticsAdapter;
    private BaseRVAdapter adapter;

    public static Intent createIntent(Context context, String OrderId,String jumpid) {
        Intent intent = new Intent(context, LogisticsActivity.class);
        intent.putExtra("OrderId", OrderId);
        intent.putExtra("jumpid", jumpid);
        return intent;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_logistics_f);
        ButterKnife.bind(this);
        OrderId = getIntent().getStringExtra("OrderId");
        jumpid = getIntent().getStringExtra("jumpid");
        actionbar.setData("物流信息", R.drawable.ic_left_back, null, 0, null,0, this);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            //改变状态栏颜色
            this.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
            actionbar.setStatusBarHeight(getStatusBarHeight());
        }
        if (jumpid.equals("1")){
            getJifenData();
        }else {
            getLogisticsData();
        }
        initView();
    }

    private void getJifenData() {
        showDialog("加载中...");
        Map<String, Object> map = new HashMap<>();
        map.put("token", Prefer.getInstance().getToken());
        map.put("integral_id", OrderId);
        XUtil.Post(Url.JIFENWULIU, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                LogUtils.e("=======商品物流详情========", result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    if (jsonObject.optString("re").equals("1")) {
                        Gson gson = new Gson();
                        LogisticsBean.InfoBean infoBean = gson.fromJson(jsonObject.optString("info"), LogisticsBean.InfoBean.class);

                        if (infoBean.getStatus().equals("0")){
                            tv_status.setText("已取消");
                        }else if (infoBean.getStatus().equals("1")){
                            tv_status.setText("待发货");
                        }else if (infoBean.getStatus().equals("2")) {
                            tv_status.setText("已发货");
                        } else if (infoBean.getStatus().equals("3")) {
                            tv_status.setText("已完成");
                        }

                        Glide.with(LogisticsActivity.this).load(infoBean.getImg()).into(img);
                        tv_express_no.setText("运单编号：" + infoBean.getExpress_no());
                        tv_express_name.setText("承运来源：" + infoBean.getExpress_name());
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                super.onFinished();
                closeDialog();
                loadList();
            }
        });
    }

    private void loadList() {
        recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL,false));
        adapter = new BaseRVAdapter(this,expressBeanList) {
            @Override
            public int getLayoutId(int viewType) {
                return R.layout.item_logisyics;
            }

            @Override
            public void onBind(BaseViewHolder holder, final int position) {

                if(position == (expressBeanList.size() - 1)){
                    holder.setImageResource(R.id.iv_dian,R.drawable.ic_home_logis_selected2x);
                }else{
                    holder.setImageResource(R.id.iv_dian,R.drawable.ic_home_logis_normal2x);
                }

                holder.setTextView(R.id.tv_name,expressBeanList.get(position).getAcceptStation());
                holder.setTextView(R.id.tv_time,expressBeanList.get(position).getAcceptTime());
            }
        };
        recyclerView.setAdapter(adapter);
        adapter.notifyDataSetChanged();
    }

    private void getLogisticsData() {
        showDialog("加载中...");
        Map<String, Object> map = new HashMap<>();
        map.put("token", Prefer.getInstance().getToken());
        map.put("orderId", OrderId);
        XUtil.Post(Url.ORDER_VIEWLOGISTICS, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                LogUtils.e("=======商品物流详情========", result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    if (jsonObject.optString("re").equals("1")) {
                        Gson gson = new Gson();
                        LogisticsBean.InfoBean infoBean = gson.fromJson(jsonObject.optString("info"), LogisticsBean.InfoBean.class);

                        if (infoBean.getOrder_status().equals("3")) {
                            tv_status.setText("配送中");
                        } else if (infoBean.getOrder_status().equals("4")) {
                            tv_status.setText("已签收");
                        }

                        Glide.with(LogisticsActivity.this).load(infoBean.getImg()).into(img);
                        tv_express_no.setText("运单编号：" + infoBean.getExpress_no());
                        tv_express_name.setText("承运来源：" + infoBean.getExpress_name());
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                super.onFinished();
                closeDialog();
            }
        });
    }

    private void initView() {
        logisticsAdapter = new LogisticsAdapter(LogisticsActivity.this, expressBeanList, OrderId);
        recyclerView.setLayoutManager(new LinearLayoutManager(LogisticsActivity.this));
        logisticsAdapter.setOnDataLoadFinish(this);
        logisticsAdapter.loadDataFirstTime();
        recyclerView.setAdapter(logisticsAdapter);
    }

    @Override
    public void onLeftClick() {
        finish();
    }

    @Override
    public void onRightClick() {

    }

    @Override
    public void onEditTextClick() {

    }

    @Override
    public void loadDataFinished(int datasLength) {
        if (datasLength == 0) {
            ll_logistics.setVisibility(View.GONE);
            recyclerView.setVisibility(View.GONE);
            layEmtpy.setVisibility(View.VISIBLE);
            tv_msg.setText("商品正在打包出库~");
        } else {
            ll_logistics.setVisibility(View.VISIBLE);
            recyclerView.setVisibility(View.VISIBLE);
            layEmtpy.setVisibility(View.GONE);
        }
    }
}
